// pages/user/login/login.js
import {
  login,
  obtain_openid
} from '../../../api.js'
var app = getApp(); //获取App内部的信息
Page({

  /* 页面的初始数据*/
  data: {
    colors:false,
    colora:false,
    isAuth: true,
    phone: '',
    password: '',
    inp_password: '',
    inp_phone: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.checkUserInfo()
    
  },
  //
  focust(e){
    if (e.detail.value !=""){
      return
    }
    this.setData({
      inp_phone:""
    })
  },
  focusp(e) {
    if (e.detail.value != "") {
      return
    }
    this.setData({
      inp_password: ""
    })
  },
  bindgetuserinfo(){
    this.checkUserInfo()
  },
  // 检查用户信息授权
  checkUserInfo() {
    const xp = this;
    wx.getSetting({
      success: function(res) {
        if (!res.authSetting['scope.userInfo']) {
          xp.setData({
            isAuth: false
          })
        } else {
          xp.setData({
            isAuth: true
          })
        }
        wx.getUserInfo({
          success(res) {
            app.globalData.userInfo = res.userInfo;
            wx.login({
              success: res => {
                obtain_openid({
                  code: res.code
                }).then(res => {
                  console.log(res)
                  app.globalData.appid = res.data.data.openid;
                })
              }
            })
          }
        })
      }
    })
  },
  forget(){
    wx.navigateTo({
      url: '../forgetPwd/forgetPwd',
    })
  },

  login() {
    var that = this
    login({
      phone: this.data.phone,
      password: this.data.password
    }).then(res => {
      console.log(res)
      if (res.data.code == 666) {
        app.globalData.business_id = res.data.data.data.id
        if (res.data.data.data.is_name_updated==1){
          app.globalData.is_name_updated=1
        }
        if (res.data.data.data.is_identify>0){
          app.globalData.is_identify = 1
        }
        if (res.data.data.data.is_auth>0){
          app.globalData.is_auth = res.data.data.data.is_auth
        }
        if (res.data.data.data.is_bank_auth>0){
          app.globalData.is_bank_auth=1
        }
       
        var num = res.data.data.data.is_auth;
        app.globalData.token = res.data.data.token;
        app.globalData.guojia = res.data.data.data.business_country ? res.data.data.data.business_country:'';
        wx.setStorageSync('phone', that.data.phone)
        wx.setStorageSync('password', that.data.password)
        wx.setStorageSync('token', res.data.data.token)
        if (res.data.code == 666){
          wx.switchTab({
            url: '../../../pages/fullStock/fullStock',
          })
        }
      }else{
        wx.showToast({
          title:res.data.msg,
          icon:"none"
        })
      }
      })
  },
  syncData(e) {

    let {
      key
    } = e.target.dataset;
    let val = e.detail.value;
    this.setData({
      [key]: val,
      colors:true
    })
  },
  syncDatas(e) {

    let {
      key
    } = e.target.dataset;
    let val = e.detail.value;
    this.setData({
      [key]: val,
      colora: true
    })
  },
  onShareAppMessage: function() {
    return {
      title: '赋能奢商，只为业绩增长',
      imageUrl: "https://document.growthupup.com/growthShare.jpg?t=" + new Date().getTime(),
      path: '/pages/user/login/login',
    }
  }

})